﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MaxStudio.Concrete.DBUtility;
using MaxStudio.Concrete.Server.Dao;
using MaxStudio.Concrete.Server.Interface; 

namespace MaxStudio.Concrete.Server.Service
{
    public class SalesOrderService : ServiceHandle, ISalesOrder
    {
        #region ISalesOrder Members

       // ConcreteDaoDataContext dataContext = new ConcreteDaoDataContext();

        public OperationResult<List<tb_SalesOrder>> GetAllSalesOrder()
        { 
            OperationResult<List<tb_SalesOrder>> result = new OperationResult<List<tb_SalesOrder>>();
            ConcreteDaoDataContext dataContext = getDateContext();
            List<tb_SalesOrder> list = new List<tb_SalesOrder>();
            try
            {
                list = dataContext.GetAllSalesOrder();
                if (list == null)
                {
                    result.errorMessageKey = "Error";
                }
                else
                {
                    result.obj = list;
                    result.operationSuccess = true;
                }
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);

            }
            return result;
        }

        public OperationResult<tb_SalesOrder> GetSalesOrderById(int id)
        {
            OperationResult<tb_SalesOrder> result = new OperationResult<tb_SalesOrder>();
            ConcreteDaoDataContext dataContext = getDateContext();
            tb_SalesOrder salesOrder;
            try
            {
                salesOrder = dataContext.GetSalesOrderById(id);
                if (salesOrder == null)
                {
                    result.errorMessageKey = "Error";
                }
                else
                {
                    result.obj = salesOrder;
                    result.operationSuccess = true;
                }
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);

            }
            return result;
        }

        public OperationResult<tb_SalesOrder> GetSalesOrderByContranctSN(string strContranctSN)
        {
            OperationResult<tb_SalesOrder> result = new OperationResult<tb_SalesOrder>();
            ConcreteDaoDataContext dataContext = getDateContext();
            tb_SalesOrder salesOrder;
            try
            {
                salesOrder = dataContext.GetSalesOrderByContranctSN(strContranctSN);
                if (salesOrder == null)
                {
                    result.errorMessageKey = "Error";
                }
                else
                {
                    result.obj = salesOrder;
                    result.operationSuccess = true;
                }
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);

            }
            return result;
        }

        public OperationResult<tb_SalesOrder> CreateSalesOrder(tb_SalesOrder entity)
        {
            OperationResult<tb_SalesOrder> result = new OperationResult<tb_SalesOrder>();
            ConcreteDaoDataContext dataContext = getDateContext();

            try
            {
                dataContext.CreateSalesOrder(entity);
                dataContext.SubmitChanges();
                result.obj = entity;
                result.operationSuccess = true;

                OperationResult<tb_Mapping_OrderProduct> mappingResult = new OperationResult<tb_Mapping_OrderProduct>();
                
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);
            }

            return result;
        }

        public OperationResult<tb_Mapping_OrderProduct> CreateOrderProductMapping(tb_Mapping_OrderProduct entity)
        {
            OperationResult<tb_Mapping_OrderProduct> result = new OperationResult<tb_Mapping_OrderProduct>();
            ConcreteDaoDataContext dataContext = getDateContext();

            try
            {
                dataContext.CreateOrderProductMapping(entity);
                dataContext.SubmitChanges();
                result.obj = entity;
                result.operationSuccess = true; 
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);
            }

            return result;
        }

        public OperationResult<tb_SalesOrder> ModifySalesOrder(tb_SalesOrder entity)
        {
            OperationResult<tb_SalesOrder> result = new OperationResult<tb_SalesOrder>();
            ConcreteDaoDataContext dataContext = getDateContext();

            try
            {
                dataContext.ModifySalesOrder(entity);
                dataContext.SubmitChanges();
                result.obj = entity;
                result.operationSuccess = true;
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);
            }

            return result;
        }

        public OperationResult<tb_SalesOrder> DeleteSalesOrder(tb_SalesOrder entity)
        {
            OperationResult<tb_SalesOrder> result = new OperationResult<tb_SalesOrder>();
            ConcreteDaoDataContext dataContext = getDateContext();
            try
            {
                dataContext.DeleteSalesOrder(entity);
                dataContext.SubmitChanges();
                result.obj = entity;
                result.operationSuccess = true;
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);
            }

            return result;
        }

        public OperationResult<tb_Mapping_OrderProduct> DeletetbMappingOrderProduct(tb_Mapping_OrderProduct entity)
        {
            OperationResult<tb_Mapping_OrderProduct> result = new OperationResult<tb_Mapping_OrderProduct>();
            ConcreteDaoDataContext dataContext = getDateContext();
            try
            {
                dataContext.DeleteOrderProductMapping(entity);
                dataContext.SubmitChanges();
                result.obj = entity;
                result.operationSuccess = true;
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);
            }

            return result;
        } 

        public OperationResult<List<tb_Mapping_OrderProduct>> GetMappingOrderProductByOrderId(int orderId)
        {
            OperationResult<List<tb_Mapping_OrderProduct>> result = new OperationResult<List<tb_Mapping_OrderProduct>>();
            ConcreteDaoDataContext dataContext = getDateContext();
            List<tb_Mapping_OrderProduct> list = new List<tb_Mapping_OrderProduct>();
            try
            {
                list = dataContext.GetMappingOrderProductByOrderId(orderId);
                if (list == null)
                {
                    result.errorMessageKey = "Error";
                }
                else
                {
                    result.obj = list;
                    result.operationSuccess = true;
                }
            }
            catch (Exception ex)
            {
                result.exceptionString = DlHelper.ExceptionString(ex);

            }
            return result;
        }

        #endregion
    }
}
